package com.chenglai.seckill.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.seckill.service.SeckillService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/23 16:22
 */
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Resource
    private SeckillService seckillService;


    @PostMapping("/start")
    @ApiOperation(value = "秒杀活动开始")
    public Result start(@ApiParam(name = "seckillId", value = "活动id") @RequestParam("seckillId") Integer seckillId,
                        @ApiParam(name = "userId", value = "用户id") @RequestParam("userId") Integer userId,
                        @ApiParam(name = "goodsId", value = "商品id") @RequestParam("goodsId") Integer goodsId,
                        @ApiParam(name = "shopId", value = "门店id") @RequestParam("shopId") Integer shopId,
                        @ApiParam(name = "price", value = "单价") @RequestParam("price") Double price,
                        @ApiParam(name = "number", value = "数量") @RequestParam("number") Integer number) {
        try {
            Map map = seckillService.start(seckillId, userId, goodsId, shopId, price, number);
            return new Result(true, StatusCode.OK, "操作成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @PostMapping("/end")
    @ApiOperation(value = "秒杀活动结束")
    public Result end(@ApiParam(name = "seckillId", value = "活动id") @RequestParam("seckillId") Integer seckillId) {
        try {
            seckillService.end(seckillId);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }


}
